﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Collections.Specialized;
using SqEngine.AI;
using sqengine.Components;
using SEEditor.Extended;

namespace SEEditor.Extended
{
    public partial class frmIntro : SEEditor.frmMDI
    {
        SaveManager _saveman;
        OpenFileDialog OpDialog;
        SaveFileDialog SvDialog;
        IntroContent _current_intro;

        public frmIntro(SaveManager saveman, SaveFileDialog SDialog, OpenFileDialog ODialog)
        {
            InitializeComponent();
            _saveman = saveman;

            OpDialog = ODialog;
            SvDialog = SDialog;
        }

        private void frmIntro_Load(object sender, EventArgs e)
        {
            lstIntroClear();
        }


        private void lstIntroClear()
        {
            lstIntro.Items.Clear();

        }

        private void bntLoad_Click(object sender, EventArgs e)
        {
            OpDialog.Title = "Open intro file";
            OpDialog.ShowDialog();

            if (OpDialog.FileName != "")
            {
                LoadIntro(OpDialog.FileName);

            }
        }

        private void LoadIntro(string filename)
        {
            lstIntroClear();
            try
            {
                _current_intro = _saveman.LoadIntro(filename);
                edtBackSong.Text = _current_intro.BackgroundSong;

                foreach (PresentationMessage item in _current_intro.IntroText)
                {
                    GListBoxItem itm = new GListBoxItem(item.text.Replace('\n', '#'), 0);

                    lstIntro.Items.Add(itm);

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }

        }

        private void bntUp_Click(object sender, EventArgs e)
        {
            if (lstIntro.SelectedIndex != -1)
            {

                int idx = lstIntro.SelectedIndex;
                if (idx != -1)
                {
                    GListBoxItem itm = (GListBoxItem)(lstIntro.SelectedItem);

                    lstIntro.Items.Remove(itm);
                    PresentationMessage pm = _current_intro.IntroText[idx];

                    _current_intro.IntroText.RemoveAt(idx);
                    lstIntro.Items.Insert(idx - 1, itm);
                    _current_intro.IntroText.Insert(idx - 1, pm);
                    lstIntro.SelectedIndex = idx - 1;
                }

            }
        }

        private void bntDown_Click(object sender, EventArgs e)
        {
            if (lstIntro.SelectedIndex != -1)
            {

                int idx = lstIntro.SelectedIndex;
                if (idx != lstIntro.Items.Count)
                {
                    GListBoxItem itm = (GListBoxItem)(lstIntro.SelectedItem);

                    lstIntro.Items.Remove(itm);
                    PresentationMessage pm = _current_intro.IntroText[idx];

                    _current_intro.IntroText.RemoveAt(idx);
                    lstIntro.Items.Insert(idx + 1, itm);
                    _current_intro.IntroText.Insert(idx + 1, pm);
                    lstIntro.SelectedIndex = idx + 1;
                }

            }
        }

        private void bntDelete_Click(object sender, EventArgs e)
        {
            if (lstIntro.SelectedIndex != -1)
            {
                _current_intro.IntroText.RemoveAt(lstIntro.SelectedIndex);
                lstIntro.Items.RemoveAt(lstIntro.SelectedIndex);
            }
        }

 

    }
}
